package com.example.dbmysql.Controller;


import com.example.dbmysql.pojo.RoomList;
import com.example.dbmysql.pojo.RoomType;
import com.example.dbmysql.result.PageResult;
import com.example.dbmysql.result.Result;
import com.example.dbmysql.service.RoomListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/roomList")
@Validated
public class roomListController {

    @Autowired
    private RoomListService roomListService;

    @PutMapping("/update")
    public  Result update(@RequestBody @Validated RoomList roomList){

        if(ArrIsEmpty(roomList.getId())) return Result.error("更新的数据不存在");

        roomListService.update(roomList);

        return  Result.success();
    }

    @GetMapping("/getAll")
    public PageResult getAll(@RequestParam int page, @RequestParam int pageSize){

        PageInfo<RoomList> pageInfo =  roomListService.SelectAll(page,pageSize);

        return PageResult.success(pageInfo.getList(), pageInfo.getTotal(), page, pageSize);
    }

    @GetMapping("/getRoomById/{id}")
    public Result getRoomById(@PathVariable("id") @Validated Integer id){

        List<RoomList> list = roomListService.selectById(id);

        return Result.success(list);
    }


    @DeleteMapping("/del/{id}")
    public Result delroom(@PathVariable("id") @Validated Integer id){

        if(ArrIsEmpty(id)) return Result.error("删除的数据不存在");

        roomListService.delectById(id);

        return  Result.success();
    }

    @DeleteMapping("/delAll")
    public Result delAll(@RequestBody Map<String, Object> ids){

        System.out.println(ids.get("ids"));
        String idsString = (String) ids.get("ids");
        // 将字符串分割成数组
        String[] idsArray = idsString.split(",");
        // 将字符串数组转换为整数数组
        List<Integer> idsList = Arrays.stream(idsArray)
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        System.out.println(idsList);
        roomListService.deleteByIds(idsList);
        return Result.success();
    }


    @PostMapping("/batchAdd")
    public Result batchAdd(@RequestBody Map<String, Object> RoomList){

        List<RoomList> list = roomListService.GetRoomList();

        List<Map<String, Object>> roomList = (List<Map<String, Object>>) RoomList.get("list");

        List<String> roomIdsToRemove = new ArrayList<>();
        // 判断房间是否重复添加
        for (RoomList index : list) {
            // 使用Stream API检查roomList中是否存在匹配的roomId
          boolean isMatch = roomList.stream()
                    .anyMatch(item -> index.getRoomId().equals(item.get("roomId")));

            if (isMatch) {
                roomIdsToRemove.add(index.getRoomId());

            }
        }
        // 获取到了重复得roomId 进行删除
        roomList.removeIf(item -> roomIdsToRemove.contains((String) item.get("roomId")));

        if(roomList.isEmpty()) return Result.error("房间全部添加过");

        Map<String, Object> claims = new HashMap<>();

        claims.put("list",roomList);

        roomListService.batchAdd(claims);

        return Result.success();

    }


    public Boolean ArrIsEmpty (Integer id){

        List<RoomList> list = roomListService.selectById(id);

        return  list.isEmpty();
    }


    public void UpdateRoomStatus(int status,String roomName){
        roomListService.updateStatusByRoomName(roomName,status);
    }
}




